/***********************************************************************
	filename: 	CEGUIPropertyHelper.cpp
	created:	6/7/2004
	author:		Paul D Turner
	
	purpose:	Implementation of PropertyHelper methods
*************************************************************************/

#include "hgePropertyHelper.h"
#include "hgeExceptions.h"
#include "hgeImageManager.h"
#include "hgeImage.h"
#include "hgeEnums.h"

#include <cstdio>
#include <sstream>

#ifdef _MSC_VER
#define snprintf _snprintf
#endif

float PropertyHelper::stringToFloat(const String& str)
{
	using namespace std;

	float val = 0;
	sscanf(str.c_str(), " %g", &val);

	return val;
}


UINT PropertyHelper::stringToUint(const String& str)
{
	using namespace std;

	UINT val = 0;
	sscanf(str.c_str(), " %u", &val);

	return val;
}


int PropertyHelper::stringToInt(const String& str)
{
	using namespace std;

	int val = 0;
	sscanf(str.c_str(), " %d", &val);

	return val;
}


bool PropertyHelper::stringToBool(const String& str)
{
	if ((str == "True") || (str == "true"))
	{
		return true;
	}
	else
	{
		return false;
	}

}


Size PropertyHelper::stringToSize(const String& str)
{
	using namespace std;

	Size val(0,0);
	sscanf(str.c_str(), " w:%g h:%g", &val.d_x, &val.d_y);

	return val;
}


Point PropertyHelper::stringToPoint(const String& str)
{
	using namespace std;

	Point val(0,0) ;
	sscanf(str.c_str(), " x:%g y:%g", &val.d_x, &val.d_y);

	return val;
}


hgeRect PropertyHelper::stringToRect(const String& str)
{
	using namespace std;

	hgeRect val(0, 0, 0, 0);
	sscanf(str.c_str(), " l:%g t:%g r:%g b:%g", &val.d_left, &val.d_top, &val.d_right, &val.d_bottom);

	return val;
}


const Image* PropertyHelper::stringToImage(const String& str)
{
	using namespace std;

    // handle empty string case
    if (str.empty())
        return 0;

	char imageName[128];
	sscanf(str.c_str(), "set:%127s", imageName);

	const Image* image;
	try
	{
		image = &ImageManager::getSingleton().getImage(String(imageName));
	}
	catch (UnknownObjectException&)
	{
		image = 0;
	}

	return image;
}

VerticalFormatting PropertyHelper::stringToVertFormat(const String& str)
{
	if (str == "CentreAligned")
	{
		return VF_CENTRE_ALIGNED;
	}
	else if (str == "BottomAligned")
	{
		return VF_BOTTOM_ALIGNED;
	}
	else if (str == "Tiled")
	{
		return VF_TILED;
	}
	else if (str == "Stretched")
	{
		return VF_STRETCHED;
	}
	else
	{
		return VF_TOP_ALIGNED;
	}
}

HorizontalFormatting PropertyHelper::stringToHorzFormat(const String& str)
{
	if (str == "CentreAligned")
	{
		return HF_CENTRE_ALIGNED;
	}
	else if (str == "RightAligned")
	{
		return HF_RIGHT_ALIGNED;
	}
	else if (str == "Tiled")
	{
		return HF_TILED;
	}
	else if (str == "Stretched")
	{
		return HF_STRETCHED;
	}
	else
	{
		return HF_LEFT_ALIGNED;
	}
}

VerticalAlignment PropertyHelper::stringToVertAlignment(const String& str)
{
	if (str == "CentreAligned")
	{
		return VA_CENTRE;
	}
	else if (str == "BottomAligned")
	{
		return VA_BOTTOM;
	}
	else
	{
		return VA_TOP;
	}
}

HorizontalAlignment PropertyHelper::stringToHorzAlignment(const String& str)
{
	if (str == "CentreAligned")
	{
		return HA_CENTRE;
	}
	else if (str == "RightAligned")
	{
		return HA_RIGHT;
	}
	else
	{
		return HA_LEFT;
	}
}

VerticalTextFormatting PropertyHelper::stringToVertTextFormat(const String& str)
{
	if (str == "CentreAligned")
	{
		return VTF_CENTRE_ALIGNED;
	}
	else if (str == "BottomAligned")
	{
		return VTF_BOTTOM_ALIGNED;
	}
	else
	{
		return VTF_TOP_ALIGNED;
	}
}

HorizontalTextFormatting PropertyHelper::stringToHorzTextFormat(const String& str)
{
	if (str == "CentreAligned")
	{
		return HTF_CENTRE_ALIGNED;
	}
	else if (str == "RightAligned")
	{
		return HTF_RIGHT_ALIGNED;
	}
	else if (str == "Justified")
	{
		return HTF_JUSTIFIED;
	}
	else if (str == "WordWrapLeftAligned")
	{
		return HTF_WORDWRAP_LEFT_ALIGNED;
	}
	else if (str == "WordWrapCentreAligned")
	{
		return HTF_WORDWRAP_CENTRE_ALIGNED;
	}
	else if (str == "WordWrapRightAligned")
	{
		return HTF_WORDWRAP_RIGHT_ALIGNED;
	}
	else if (str == "WordWrapJustified")
	{
		return HTF_WORDWRAP_JUSTIFIED;
	}
	else
	{
		return HTF_LEFT_ALIGNED;
	}
}


String PropertyHelper::vertFormatToString(VerticalFormatting format)
{
	switch (format)
	{
	case VF_BOTTOM_ALIGNED:
		return String("BottomAligned");
		break;
	case VF_CENTRE_ALIGNED:
		return String("CentreAligned");
		break;
	case VF_TILED:
		return String("Tiled");
		break;
	case VF_STRETCHED:
		return String("Stretched");
		break;
	default:
		return String("TopAligned");
		break;
	}
}

String PropertyHelper::horzFormatToString(HorizontalFormatting format)
{
	switch (format)
	{
	case HF_RIGHT_ALIGNED:
		return String("RightAligned");
		break;
	case HF_CENTRE_ALIGNED:
		return String("CentreAligned");
		break;
	case HF_TILED:
		return String("Tiled");
		break;
	case HF_STRETCHED:
		return String("Stretched");
		break;
	default:
		return String("LeftAligned");
		break;
	}
}

String PropertyHelper::vertAlignmentToString(VerticalAlignment alignment)
{
	switch (alignment)
	{
	case VA_BOTTOM:
		return String("BottomAligned");
		break;
	case VA_CENTRE:
		return String("CentreAligned");
		break;
	default:
		return String("TopAligned");
		break;
	}
}

String PropertyHelper::horzAlignmentToString(HorizontalAlignment alignment)
{
	switch (alignment)
	{
	case HA_RIGHT:
		return String("RightAligned");
		break;
	case HA_CENTRE:
		return String("CentreAligned");
		break;
	default:
		return String("LeftAligned");
		break;
	}
}

String PropertyHelper::floatToString(float val)
{
	using namespace std;

	char buff[64];
	snprintf(buff, sizeof (buff), "%g", val);

	return String(buff);

#if 0 // slower
    std::ostringstream iss;
    iss << val;
    return String(iss.str());
#endif
}


String PropertyHelper::uintToString(UINT val)
{
	using namespace std;

	char buff[64];
	snprintf(buff, sizeof (buff), "%u", val);

	return String(buff);

#if 0 // slower
    std::ostringstream iss;
    iss << val;
    return String(iss.str());
#endif
}


String PropertyHelper::intToString(int val)
{
	using namespace std;

	char buff[64];
	snprintf(buff, sizeof (buff), "%d", val);

	return String(buff);

#if 0 // slower
    std::ostringstream iss;
    iss << val;
    return String(iss.str());
#endif
}


String PropertyHelper::boolToString(bool val)
{
	if (val)
	{
		return String("True");
	}
	else
	{
		return String ("False");
	}

}


String PropertyHelper::sizeToString(const Size& val)
{
	using namespace std;

	char buff[128];
	snprintf(buff, sizeof (buff), "w:%g h:%g", val.d_x, val.d_y);

	return String(buff);

#if 0 // slower
    std::ostringstream iss;
    iss << "w:" << val.d_x << " h:" << val.d_y;
    return String(iss.str());
#endif
}


String PropertyHelper::pointToString(const Point& val)
{
	using namespace std;

	char buff[128];
	snprintf(buff, sizeof (buff), "x:%g y:%g", val.d_x, val.d_y);

	return String(buff);

#if 0 // slower
    std::ostringstream iss;
    iss << "x:" << val.d_x << " y:" << val.d_y;
    return String(iss.str());
#endif
}


String PropertyHelper::rectToString(const hgeRect& val)
{
	using namespace std;

	char buff[256];
	snprintf(buff, sizeof (buff), "l:%g t:%g r:%g b:%g",
		     val.d_left, val.d_top, val.d_right, val.d_bottom);

	return String(buff);

#if 0 // slower
    std::ostringstream iss;
    iss << "l:" << val.d_left << " t:" << val.d_top << " r:" << val.d_right << " b:" << val.d_bottom;
    return String(iss.str());
#endif
}


String PropertyHelper::imageToString(const Image* const val)
{
	if (val)
	{
		return String("image:" + val->getName());
	}

	return String("");
}



String PropertyHelper::colourToString(const colour& val)
{
	using namespace std;

	char buff[16];
	sprintf(buff, "%.8X", val.getARGB());

	return String(buff);
}


colour PropertyHelper::stringToColour(const String& str)
{
	using namespace std;

	argb_t val = 0xFF000000;
	sscanf(str.c_str(), " %8X", &val);

	return colour(val);

}

String	hcursorToString(const HCURSOR& val)
{

}

String PropertyHelper::colourRectToString(const ColourRect& val)
{
	using namespace std;

	char buff[64];
	sprintf(buff, "tl:%.8X tr:%.8X bl:%.8X br:%.8X", val.d_top_left.getARGB(), val.d_top_right.getARGB(), val.d_bottom_left.getARGB(), val.d_bottom_right.getARGB());

	return String(buff);
}


ColourRect PropertyHelper::stringToColourRect(const String& str)
{
	using namespace std;

    if (str.length() == 8)
    {
        argb_t all = 0xFF000000;
        sscanf(str.c_str(), "%8X", &all);
        return ColourRect(all);
    }

	argb_t topLeft = 0xFF000000, topRight = 0xFF000000, bottomLeft = 0xFF000000, bottomRight = 0xFF000000;
	sscanf(str.c_str(), "tl:%8X tr:%8X bl:%8X br:%8X", &topLeft, &topRight, &bottomLeft, &bottomRight);

	return ColourRect(topLeft, topRight, bottomLeft, bottomRight);
}

//----------------------------------------------------------------------------//
Vector3 PropertyHelper::stringToVector3(const String& str)
{
    using namespace std;

    Vector3 val(0, 0, 0);
    sscanf(str.c_str(), " x:%g y:%g z:%g", &val.d_x, &val.d_y, &val.d_z);

    return val;
}

//----------------------------------------------------------------------------//
String PropertyHelper::vector3ToString(const Vector3& val)
{
    using namespace std;

    char buff[128];
    snprintf(buff, sizeof(buff), "x:%g y:%g z:%g", val.d_x, val.d_y, val.d_z);

    return String(buff);
}


